<?php
namespace app\manage\model;
use think\Db;

class Channel extends Base
{
    public function channelIdName(){
        $select1 = Db::name('ViewChannel')->field('cps_id as channel_id, cps_name as channel_name')->where('channel_id', 1000)->order('cps_id desc')->select();
        $select2 = Db::name('ViewChannel')->field('channel_id, channel_name')->where('channel_id', '>=', 5000)->order('channel_id desc')->select();
        $channel = array_merge($select2, $select1);
        $channelIdName = [];
        foreach($channel as $k=>$v){
            $channelIdName[$v['channel_id']] = $v['channel_name'];
        }
        $channelIdName[0] = '测试渠道0';
        $channelIdName[1334] = '测试渠道1334';
        $channelIdName[1278] = '测试渠道1278';
        $channelIdName[1259] = '测试渠道1259';
        return $channelIdName;
    }

    public function allChannelId($cpsId){
        //获取渠道的pid
        $pid = Db::name('ViewChannel')->where('cps_id', $cpsId)->value('pid');

        //如果是一级渠道：显示一级渠道并且同时显示二级渠道
        if($pid == 0) {
            //查找到所有二级渠道id
            $sonId = Db::name('ViewChannel')->where(['pid' => $cpsId])->column('cps_id');

            //如果查询到有二级渠道
            if ($sonId) {
                $allCpsId = join(',', $sonId);
                $allCpsId .= ',' . $cpsId;
                return $allCpsId;
            }
        }
        return $cpsId;
    }

}